﻿using System;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Ink;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using System.Collections.ObjectModel;
using System.Windows.Data;
using System.ComponentModel;
using System.Collections.Generic;
using EIA.SLClient.ProjectManage.Infrastructure;

namespace EIA.SLClient.ProjectManage.Basic
{
    public class ProjectInfoControlViewModel : SimpleMvvmToolkit.ViewModelBase<ProjectInfoControlViewModel>
    {
        public ProjectInfoControlViewModel()
        {
            if (IsInDesignMode) return;
        }

        protected bool _IsEditing;
        /// <summary>
        /// 注释
        /// </summary>
        public bool IsEditing
        {
            get { return _IsEditing; }
            set
            {
                _IsEditing = value;
                NotifyPropertyChanged(m => m.IsEditing);
            }
        }

        protected EIA.PM.Model.TProjectBasic _Model;
        /// <summary>
        /// 注释
        /// </summary>
        public EIA.PM.Model.TProjectBasic Model
        {
            get { return _Model; }
            set
            {
                _Model = value;
                NotifyPropertyChanged(m => m.Model);
                SelectDicItem();
                SelectEngineer();
            }
        }

        protected PagedCollectionView _EngineerCollection;
        /// <summary>
        /// 注释
        /// </summary>
        public PagedCollectionView EngineerCollection
        {
            get
            {
                if (_EngineerCollection == null)
                {
                    _EngineerCollection = new PagedCollectionView(ListsResource.Instance.EngineerCollection);
                    _EngineerCollection.Filter = (e) =>
                    {
                        return Model == null ? true : ((e as EIA.PM.Model.TEngineer).EngType == Model.PrjIndustry);
                    };
                }
                return _EngineerCollection;
            }
        }

        protected EIA.PM.Model.TEngineer _SelectedEngineer;
        /// <summary>
        /// 注释
        /// </summary>
        public EIA.PM.Model.TEngineer SelectedEngineer
        {
            get { return _SelectedEngineer; }
            set
            {
                _SelectedEngineer = value;
                NotifyPropertyChanged(m => m.SelectedEngineer);
                if (Model != null)
                {
                    if (_SelectedEngineer != null)
                    {
                        Model.PrjEngineer = _SelectedEngineer.StfId;
                        Model.PrjEngineerName = _SelectedEngineer.StfName;
                    }
                    else
                    {
                        Model.PrjEngineer = null;
                        Model.PrjEngineerName = null;
                    }
                }
            }
        }

        protected EIA.Basic.Model.DicItem _XMLX;
        /// <summary>
        /// 项目类型
        /// </summary>
        public EIA.Basic.Model.DicItem XMLX
        {
            get { return _XMLX; }
            set
            {
                _XMLX = value;
                NotifyPropertyChanged(m => m.XMLX);
                Model.PrjType = (value == null) ? -1 : value.DicIid;
            }
        }

        protected EIA.Basic.Model.DicItem _XMSZD;
        /// <summary>
        /// 注释
        /// </summary>
        public EIA.Basic.Model.DicItem XMSZD
        {
            get { return _XMSZD; }
            set
            {
                _XMSZD = value;
                NotifyPropertyChanged(m => m.XMSZD);
                Model.PrjRegion = (value == null) ? -1 : value.DicIid;
            }
        }

        protected EIA.Basic.Model.DicItem _XMSPBM;
        /// <summary>
        /// 注释
        /// </summary>
        public EIA.Basic.Model.DicItem XMSPBM
        {
            get { return _XMSPBM; }
            set
            {
                _XMSPBM = value;
                NotifyPropertyChanged(m => m.XMSPBM);
                Model.PrjApproveGov = (value == null) ? -1 : value.DicIid;
            }
        }

        protected EIA.Basic.Model.DicItem _HYLX;
        /// <summary>
        /// 注释
        /// </summary>
        public EIA.Basic.Model.DicItem HYLX
        {
            get { return _HYLX; }
            set
            {
                _HYLX = value;
                NotifyPropertyChanged(m => m.HYLX);
                Model.PrjIndustry = (value == null) ? -1 : value.DicIid;
                if (EngineerCollection != null)
                {
                    EngineerCollection.Refresh();
                }
            }
        }

        protected EIA.Basic.Model.DicItem _XMLY;
        /// <summary>
        /// 注释
        /// </summary>
        public EIA.Basic.Model.DicItem XMLY
        {
            get { return _XMLY; }
            set
            {
                _XMLY = value;
                NotifyPropertyChanged(m => m.XMLY);
                Model.PrjSource = (value == null) ? -1 : value.DicIid;
            }
        }

        private void SelectEngineer()
        {
            if (EngineerCollection != null && Model != null)
            {
                SelectedEngineer = (EngineerCollection.SourceCollection as IList<EIA.PM.Model.TEngineer>)
                    .FirstOrDefault((e) => { return e.StfId == Model.PrjEngineer; });
            }
            else
            {
                SelectedEngineer = null;
            }
        }
        private void SelectDicItem()
        {
            if (Model != null && ListsResource.Instance != null)
            {
                XMLX = ListsResource.Instance.XMLXCollection.FirstOrDefault((d) => { return d.DicIid == Model.PrjType; });
                XMSZD = ListsResource.Instance.XMSZDCollection.FirstOrDefault((d) => { return d.DicIid == Model.PrjRegion; });
                XMSPBM = ListsResource.Instance.XMSPBMCollection.FirstOrDefault((d) => { return d.DicIid == Model.PrjApproveGov; });
                HYLX = ListsResource.Instance.HYLXCollection.FirstOrDefault((d) => { return d.DicIid == Model.PrjIndustry; });
                XMLY = ListsResource.Instance.XMLYCollection.FirstOrDefault((d) => { return d.DicIid == Model.PrjSource; });
            }
            else
            {
                XMLX = null;
                XMSZD = null;
                XMSPBM = null;
                HYLX = null;
                XMLY = null;
            }
        }
    }
}
